package interfaces.subpanel.summaryinfo.gui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import model.SummaryGenericScenarie;

import org.apache.commons.collections.CollectionUtils;

import comparators.SummaryGenericScenarieComparator;

import enumerations.AttributesEnum;

public class ResultScenariesTable extends AbstractTableModel {

	private static final long serialVersionUID = 1L;

	private static final String SCENARIE_COLUMN_NAME = "Scenarie";

	private static final String TYPE_COLUMN_NAME = "Type";

	private static final String SUBTYPE_COLUMN_NAME = "Attribute";

	private static final String CLUSTER_COLUMN_NAME = "Cluster";

	private String[] columnNames = { SCENARIE_COLUMN_NAME, TYPE_COLUMN_NAME, SUBTYPE_COLUMN_NAME, CLUSTER_COLUMN_NAME };

	private List<SummaryGenericScenarie> data = new ArrayList<SummaryGenericScenarie>();

	private SummaryGenericScenarieComparator summaryGenericScenarieComparator = new SummaryGenericScenarieComparator();

	public int getColumnCount() {
		return this.columnNames.length;
	}

	public int getRowCount() {
		return this.data.size();
	}

	public String getColumnName(int col) {
		return this.columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		if (this.data.size() <= row) {
			System.out.println("ERROR: Out of Index while getting data from model in table.");
			return null;
		}
		SummaryGenericScenarie scenarie = this.data.get(row);
		if (col == 0)
			return (String) scenarie.getScenarie();
		else if (col == 1) {
			if (scenarie.getAttribute().equals(AttributesEnum.FUNCTIONALITY.name()))
				return "FUNCTIONALITY";
			else
				return "QUALITY";
		} else if (col == 2) {
			if (scenarie.getAttribute().equals(AttributesEnum.FUNCTIONALITY.name()))
				return "";
			else
				return (String) scenarie.getAttribute().toString();
		} else
			return scenarie.getAttribute().equals(AttributesEnum.FUNCTIONALITY.name()) ? String.valueOf(scenarie.getCluster()) : "";
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Class getColumnClass(int c) {
		return getValueAt(0, c) != null ? getValueAt(0, c).getClass() : null;
	}

	public boolean isCellEditable(int row, int col) {
		if (col == 0) {
			return true;
		} else {
			return false;
		}
	}

	public void addItem(SummaryGenericScenarie genericScenarie) {
		if (genericScenarie != null) {
			this.data.add(genericScenarie);
			Collections.sort(this.data, this.summaryGenericScenarieComparator);
			fireTableDataChanged();
		}
	}

	public void addAllGenericScenaries(List<SummaryGenericScenarie> genericScenaries) {
		if (CollectionUtils.isNotEmpty(genericScenaries)) {
			this.data.addAll(genericScenaries);
			Collections.sort(this.data, this.summaryGenericScenarieComparator);
			fireTableDataChanged();
		}
	}

	public void setValueAt(Object value, int row, int col) {
		if (data.size() <= row) {
			System.out.println("ERROR: Out of Index while setting data into data model in table.");
			return;
		}
		if (col == 0)
			data.get(row).setScenarie(((SummaryGenericScenarie) value).getScenarie());
		else if (col == 1 || col == 2) {
			data.get(row).setAttribute(((SummaryGenericScenarie) value).getAttribute());
		} else
			data.get(row).setCluster(((SummaryGenericScenarie) value).getCluster());
		fireTableCellUpdated(row, col);
	}

	public int dataSize() {
		return this.data.size();
	}

	public boolean isEmpty() {
		return CollectionUtils.isEmpty(this.data);
	}

	public boolean isNotEmpty() {
		return !this.isEmpty();
	}

	public SummaryGenericScenarie getGenericScenarie(int index) {
		SummaryGenericScenarie genericScenarie = null;
		if (this.data.size() > index)
			genericScenarie = this.data.get(index);
		return genericScenarie;
	}

	public void removeGenericScenarie(int index) {
		if (this.data.size() > index) {
			this.data.remove(index);
			fireTableDataChanged();
		}
	}

	public List<SummaryGenericScenarie> removeIndexes(int[] genericScenariesIndexes) {

		List<SummaryGenericScenarie> removedScenaries = new ArrayList<SummaryGenericScenarie>();
		List<SummaryGenericScenarie> notRemovedScenaries = new ArrayList<SummaryGenericScenarie>();
		if (CollectionUtils.isNotEmpty(this.data)) {
			int i = 0, j = 0;
			while (i < genericScenariesIndexes.length) {
				int indexToRemove = genericScenariesIndexes[i];
				while (j < this.data.size()) {
					if (j == indexToRemove) {
						removedScenaries.add(this.data.get(indexToRemove));
						if (i < genericScenariesIndexes.length - 1)
							break;
					} else
						notRemovedScenaries.add(new SummaryGenericScenarie(this.data.get(indexToRemove).getCluster(), this.data.get(indexToRemove)
								.getScenarie(), this.data.get(indexToRemove).getAttribute()));
					j++;
				}
				i++;
			}
			this.data.removeAll(removedScenaries);
			Collections.sort(this.data, this.summaryGenericScenarieComparator);
			fireTableDataChanged();
		}

		return removedScenaries;
	}

	public void removeAllGenericScenaries() {
		this.data.clear();
		//fireTableDataChanged();
	}

	public List<SummaryGenericScenarie> getAllGenericScenaries() {
		if (this.data == null)
			this.data = new ArrayList<SummaryGenericScenarie>();
		return this.data;
	}

}
